package com.yiqi.lottery.feature.mine.view;

import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import butterknife.BindView;
import butterknife.OnClick;
import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.PowerfulEditText;
import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.TokenBo;
import com.yiqi.lottery.feature.mine.contract.MineContract;
import com.yiqi.lottery.feature.mine.dependencies.mine.DaggerMineComponent;
import com.yiqi.lottery.feature.mine.dependencies.mine.MinePresenterModule;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

import javax.inject.Inject;
import java.util.concurrent.TimeUnit;

public class VerifiedActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener, MineContract.View {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;

    @Inject

    MineContract.Presenter mPresenter;
    @BindView(R.id.edt_user_auth_realname)
    PowerfulEditText mEdtUserAuthRealname;
    @BindView(R.id.edt_user_auth_idcard)
    PowerfulEditText mEdtUserAuthIdcard;
    @BindView(R.id.edt_user_auth_mobile)
    PowerfulEditText mEdtUserAuthMobile;
    @BindView(R.id.edt_user_auth_code)
    PowerfulEditText mEdtUserAuthCode;
    @BindView(R.id.btn_user_auth_getcode)
    Button mBtnUserAuthGetcode;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_verified;
    }

    @Override
    protected void initInjector() {
        DaggerMineComponent.builder().minePresenterModule(new MinePresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        mToolbar.inflateMenu(R.menu.menu_user_contact_service);
        mToolbar.setOnMenuItemClickListener(this);
        initToolBar(mToolbar, "实名认证", true, true);
    }

    @Override
    protected void initData() {

    }


    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if (item.getItemId() == R.id.action_edit) {
            T.showShort("联系客服");
        }
        return false;
    }

    @Override
    public void showUserInfoSuccess(UserInfoBo userInfoBo) {

    }

    @Override
    public void showAuthSuccess(String msg) {
       T.showShort("实名认证成功！");
       finish();
    }

    @Override
    public void showSendCodeSuccess(String code) {
        T.showShort("发送验证码成功！");
        onGetCode();
    }

    @Override
    public void showEditPwdSuccess(String msg) {

    }

    @Override
    public void showSendCodeError(String error) {
        T.showShort(error);
    }

    @Override
    public void showError(String error) {
        T.showShort(error);
    }

    @Override
    public void showQiniuTokenSuccess(TokenBo data) {

    }

    @Override
    public void showUploadHashSuccess(String hash) {

    }




    @OnClick({R.id.btn_user_auth_getcode, R.id.btn_user_auth_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_user_auth_getcode:
                mPresenter.doUserSendCode(mEdtUserAuthMobile.getText().toString());
                break;
            case R.id.btn_user_auth_commit:
                mPresenter.doUserAuth(mEdtUserAuthIdcard.getText().toString(),
                        mEdtUserAuthRealname.getText().toString(),
                        mEdtUserAuthMobile.getText().toString(),
                        mEdtUserAuthCode.getText().toString()
                );
                break;
        }
    }

    public void onGetCode() {
        final int count = 60;//倒计时10秒
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count + 1)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return count - aLong;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//ui线程中进行控件更新
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        mBtnUserAuthGetcode.setEnabled(false);
                        mBtnUserAuthGetcode.setTextColor(AM.res().getColor(R.color.text_secondary));
                        mBtnUserAuthGetcode.setBackgroundResource(R.drawable.common_normal_bg);
                    }
                }).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Long num) {

                mBtnUserAuthGetcode.setText( num + "s");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                //回复原来初始状态
                mBtnUserAuthGetcode.setEnabled(true);
                mBtnUserAuthGetcode.setText("发送验证码");
                mBtnUserAuthGetcode.setTextColor(AM.res().getColor(R.color.primary_comparison));
                mBtnUserAuthGetcode.setBackgroundResource(R.drawable.common_primary_bg);
            }
        });

    }
}
